package org.gdsdxy.pmmp.dao;

import java.util.ArrayList;
import java.util.List;

import org.gdsdxy.pmmp.entity.MemberBaseInfo;
import org.gdsdxy.pmmp.entity.MemberImge;
import org.gdsdxy.pmmp.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;



public class UserManagerDaoImpl implements IUserManagerDao{

	public MemberBaseInfo queryMemberBaseInfo(String userName, String password) {
		Session session = HibernateUtil.createSession();
		String hql="from MemberBaseInfo where userName='"+userName+"' and password='"+password+"'";
		Query query =session.createQuery(hql);
		List result =query.list();
		if(result.size()>0)
			return (MemberBaseInfo)result.get(0);
		
		return null;
	}

	public boolean saveMemberImge(MemberImge imge) {
		Session session = HibernateUtil.createSession();
		try{
			session.beginTransaction();
			session.save(imge);
			session.beginTransaction().commit();
			return true;
		}catch(HibernateException e){
			e.printStackTrace();
			session.beginTransaction().rollback();
		}finally{
			HibernateUtil.closeSession(session);
		}
		return false;
	}

	public List<MemberImge> queryImgeByUserId(Integer id) {
		Session session = HibernateUtil.createSession();
		List<MemberImge> imgeList=null;
		try{
			String hql="from MemberImge where userid="+id;
			Query query =session.createQuery(hql);
			imgeList=query.list();
			
		}catch(HibernateException e){
			e.printStackTrace();
			
		}finally{
			HibernateUtil.closeSession(session);
		}
		return imgeList;
	}

	public long getImageCountById(Integer id) {
		Session session = HibernateUtil.createSession();
		long count=0;
		try{
			String hql="select count(*) from MemberImge where userid="+id;
			Query query=session.createQuery(hql);
			List list = query.list();
			if(list!=null && list.size()>0){
				count=(Long)list.get(0);
			}
		}catch(HibernateException e){
			
			e.printStackTrace();
		}finally{
			HibernateUtil.closeSession(session);
		}
		return count;
	}

	public List<MemberImge> queryImgeByUserIdFy(int start, int size, Integer id) {
		System.out.println(start+" "+size);
		Session session = HibernateUtil.createSession();
		List<MemberImge> images = new ArrayList<MemberImge>();
		try{
			String hql="from MemberImge where userid="+id;
			Query query = session.createQuery(hql);
			query.setFirstResult(start);
			query.setMaxResults(size);
			images=query.list();
			
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateUtil.closeSession(session);
		}
		return images;
	}

  public List<MemberBaseInfo> queryUsers(int start,int size) {
    Session session = HibernateUtil.createSession();
    List<MemberBaseInfo> users = new ArrayList<MemberBaseInfo>();
    try{
      String hql="from MemberBaseInfo";
      Query query = session.createQuery(hql);
      query.setFirstResult(start);
      query.setMaxResults(size);
      users=query.list();
      
    }catch(HibernateException e){
      e.printStackTrace();
    }finally{
      HibernateUtil.closeSession(session);
    }
    return users;
  }

@Override
public boolean saveMemberBaseInfo(MemberBaseInfo baseInfo) {
	// TODO Auto-generated method stub
	return false;
}

public MemberBaseInfo get(Integer id) {
  // TODO Auto-generated method stub
  return null;
}
	
	

}
